home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util1 / reboot12.lha / Reboot / Reboot.c < prev    next >
C/C++ Source or Header  |  1995-11-22  |  14KB  |  614 lines

  1. ; /*
  2. CatComp Reboot.cd CFILE Reboot_strings.h NOARRAY NOBLOCK NOCODE
  3. CatComp Reboot.cd CTFILE Reboot_blank.ct
  4. CatComp Reboot.cd Reboot_deutsch.ct CATALOG Catalogs/Deutsch/Reboot.catalog
  5.  
  6. ; DICE
  7. dcc Reboot.c -r -mS -mi -proto -v -oReboot
  8.  
  9. ; SAS
  10. ; SC RESOPT PARM=REGISTERS UCHAR CONSTLIB STREQ ANSI NOSTKCHK NOICONS OPT OPTPEEP Reboot.c
  11. ; Slink LIB:c.o Reboot.o TO Reboot LIB LIB:sc.lib SC SD
  12.  
  13. Quit
  14. */
  15.  
  16. /*
  17. **
  18. **  $VER: Reboot.c 1.2 (22.11.95)
  19. **  Reboot 1.0
  20. **
  21. **  main file
  22. **
  23. **  (C) Copyright 1994/1995 by Roland 'Gizzy' Mainz
  24. **          All Rights Reserved
  25. **
  26. */
  27.  
  28. /* We did not want any names before V36 */
  29. #define INTUI_V36_NAMES_ONLY 1
  30.  
  31. /* amiga includes */
  32. #include <exec/types.h>
  33. #include <utility/utility.h>
  34. #include <dos/dos.h>
  35. #include <intuition/intuition.h>
  36. #include <workbench/startup.h>
  37. #include <workbench/icon.h>
  38. #include <libraries/locale.h>
  39.  
  40. /* amiga prototypes */
  41. #include <clib/exec_protos.h>
  42. #include <clib/utility_protos.h>
  43. #include <clib/dos_protos.h>
  44. #include <clib/intuition_protos.h>
  45. #include <clib/icon_protos.h>
  46. #include <clib/locale_protos.h>
  47.  
  48. /* DICE inline stuff */
  49. #ifdef __DICE_INLINE
  50. #define UtilityBase_DECLARED 1
  51. #define IntuitionBase_DECLARED 1
  52. #define IconBase_DECLARED 1
  53. #define LocaleBase_DECLARED 1
  54.  
  55. #include <proto/exec_protos.h>
  56. #include <proto/utility_protos.h>
  57. #include <proto/dos_protos.h>
  58. #include <proto/intuition_protos.h>
  59. #include <proto/icon_protos.h>
  60. #include <proto/locale_protos.h>
  61. #endif /* __DICE_INLINE */
  62.  
  63. /* ansi includes */
  64. #include <string.h>
  65.  
  66. /* version string */
  67. #include "Reboot_rev.h"
  68.  
  69. /* locale */
  70. #define CATCOMP_NUMBERS
  71. #define CATCOMP_STRINGS
  72. #include "Reboot_strings.h"
  73.  
  74. /* misc defines */
  75. #define NAME "Reboot"
  76.  
  77. /* prototypes */
  78.                 void                  chkabort( void );
  79.  
  80. #ifdef _DCC
  81.                 long                  wbmain( struct WBStartup * );
  82. #endif /* _DCC */
  83.  
  84.                 long                  main( long, STRPTR * );
  85.         static  void                  DefaultSettings( void );
  86.         static  void                  FreeInitProjectResult( void );
  87.  
  88.         static  void                  ClearRDA( void );
  89.         static  void                  ScanRDA( void );
  90.         static  void                  ScanToolTypes( STRPTR * );
  91.  
  92.         static  void                  RunTool( void );
  93.         static  BOOL                  OpenLibStuff( void );
  94.         static  void                  CloseLibStuff( void );
  95.         static  BOOL                  CreateBasicResources( void );
  96.         static  void                  DeleteBasicResources( void );
  97.                 void                  AttemptOpenLibrary( struct Library **, STRPTR, STRPTR, ULONG );
  98.         static  void                  RunTool( void );
  99.         static  void                  MyReboot( void );
  100.                 STRPTR                SafeGetCatalogStr( struct Catalog *, LONG, STRPTR );
  101.  
  102.  
  103.  
  104. /* version_string */
  105. STRPTR versionstring = VERSTAG;
  106.  
  107. long main_retval,
  108.      main_retval2;
  109.  
  110. static struct RDArgs  *startuprda;
  111.  
  112. /* shared libraries */
  113. struct Library *UtilityBase,
  114.                *IntuitionBase,
  115.                *IconBase,
  116.                *LocaleBase;
  117.  
  118. /* locale support */
  119. struct Catalog *ct;
  120.  
  121. /* template for ReadArgs() */
  122. #define STARTUP_TEMPLATE "REBOOTDELAY/K/N,QUIET=NOREQ/S"
  123.  
  124. static
  125. struct
  126. {
  127.     long *rebootdelay;
  128.     long *noreq;
  129. } result;
  130.  
  131. static
  132. struct
  133. {
  134.     ULONG rebootdelay;
  135.     ULONG noreq;
  136. } project;
  137.  
  138.  
  139. /* disable CTRL_C break support (DICE CTRL_C abort function) */
  140. void chkabort( void )
  141. {
  142. }
  143.  
  144.  
  145. /* DICE workbench entry */
  146. #ifdef _DCC
  147. long wbmain( struct WBStartup *wbstartup )
  148. {
  149.     /* Call main like SAS-C */
  150.     return( main( 0L, (STRPTR *)wbstartup ) );
  151. }
  152. #endif /* _DCC */
  153.  
  154.  
  155. long main( long ac, STRPTR *av )
  156. {
  157.     LONG               numArgs,
  158.                        x;
  159.     struct WBStartup  *wbstartup;
  160.     struct WBArg      *wbarg;
  161.     struct DiskObject *tooldobj,
  162.                       *projectdobj;
  163.     BPTR               oldToolLock,
  164.                        oldProjectLock;
  165.  
  166.     x = main_retval2 = 0L;
  167.     main_retval = RETURN_OK;
  168.  
  169.     DefaultSettings();
  170.  
  171. /* Workbench */
  172.     if( ac == 0L )
  173.     {
  174.       wbstartup = (struct WBStartup *)av;
  175.  
  176.       if( CreateBasicResources() )
  177.       {
  178.         numArgs = wbstartup -> sm_NumArgs;
  179.         wbarg   = wbstartup -> sm_ArgList;
  180.  
  181.         if( *(wbarg[ 0 ] . wa_Name) )
  182.         {
  183.           if( wbarg[ 0 ] . wa_Lock )
  184.           {
  185.             oldToolLock = CurrentDir( (wbarg[ 0 ] . wa_Lock) );
  186.           }
  187.  
  188.           if( tooldobj = GetDiskObjectNew( (wbarg[ 0 ] . wa_Name) ) )
  189.           {
  190.             /* two possible cases when started from workbench ... */
  191.             if( numArgs < 2L )
  192.             {
  193.               /* ... first case, only our tool icon is given, create one project here */
  194.  
  195.               ScanToolTypes( (STRPTR *)(tooldobj -> do_ToolTypes) );
  196.  
  197.               RunTool();
  198.  
  199.               FreeInitProjectResult();
  200.             }
  201.             else
  202.             {
  203.               /* ... second case, a couple of project icons are given, multiple projects will start from here */
  204.               for( x = 1L ; x < numArgs ; x++ )
  205.               {
  206.                 if( wbarg[ x ] . wa_Lock )
  207.                 {
  208.                   oldProjectLock = CurrentDir( (wbarg[ x ] . wa_Lock) );
  209.                 }
  210.  
  211.                 if( *(wbarg[ x ] . wa_Name) )
  212.                 {
  213.                   if( projectdobj = GetDiskObject( (wbarg[ x ] . wa_Name) ) )
  214.                   {
  215.                     ScanToolTypes( (STRPTR *)(tooldobj -> do_ToolTypes) );
  216.                     ScanToolTypes( (STRPTR *)(projectdobj -> do_ToolTypes) );
  217.  
  218.                     RunTool();
  219.  
  220.                     FreeInitProjectResult();
  221.                     DefaultSettings();
  222.  
  223.                     FreeDiskObject( projectdobj );
  224.                   }
  225.                 }
  226.  
  227.                 if( wbarg[ x ] . wa_Lock )
  228.                 {
  229.                   CurrentDir( oldProjectLock );
  230.                 }
  231.               }
  232.             }
  233.  
  234.             FreeDiskObject( tooldobj );
  235.           }
  236.  
  237.           if( wbarg[ 0 ] . wa_Lock )
  238.           {
  239.             CurrentDir( oldToolLock );
  240.           }
  241.         }
  242.  
  243.         DeleteBasicResources();
  244.       }
  245.     }
  246.     else
  247.     {
  248. /* CLI/Shell */
  249.       if( CreateBasicResources() )
  250.       {
  251.         if( startuprda = ReadArgs( STARTUP_TEMPLATE, (LONG *)(&result), NULL ) )
  252.         {
  253.           /* did we get a CTRL_C signal ? */
  254.           if( !CheckSignal( SIGBREAKF_CTRL_C ) )
  255.           {
  256.             ScanRDA();
  257.  
  258.             RunTool();
  259.  
  260.             FreeInitProjectResult();
  261.           }
  262.           else
  263.           {
  264.             main_retval2 = ERROR_BREAK;
  265.             main_retval  = RETURN_WARN;
  266.           }
  267.  
  268.           FreeArgs( startuprda );
  269.         }
  270.         else
  271.         {
  272.           main_retval2 = IoErr();
  273.           main_retval  = RETURN_ERROR;
  274.         }
  275.  
  276.         PrintFault( main_retval2, NAME );
  277.  
  278.         DeleteBasicResources();
  279.       }
  280.     }
  281.  
  282.     SetIoErr( main_retval2 );
  283.  
  284.     return( main_retval );
  285. }
  286.  
  287.  
  288. void DefaultSettings( void )
  289. {
  290.     ClearRDA();
  291.  
  292.     memset( (&project), 0, sizeof(project) );
  293.  
  294.     project . rebootdelay = 2UL;
  295. }
  296.  
  297.  
  298. void ClearRDA( void )
  299. {
  300.     memset( (&result), 0, sizeof(result) );
  301. }
  302.  
  303.  
  304. void ScanRDA( void )
  305. {
  306.     if( result . rebootdelay )
  307.     {
  308.       project . rebootdelay = (ULONG)(*(result . rebootdelay));
  309.     }
  310.  
  311.     project . noreq = result . noreq;
  312.  
  313.     ClearRDA();
  314. }
  315.  
  316.  
  317. void ScanToolTypes( STRPTR *tt )
  318. {
  319.     STRPTR s;
  320.  
  321.     if( s = FindToolType( tt, "REBOOTDELAY" ) )
  322.       (void)StrToLong( s, (LONG *)(&(project . rebootdelay)) ); /* note: result ignored */
  323.  
  324.     if( s = FindToolType( tt, "NOREQ" ) )
  325.       result . noreq = (long *)s;
  326.  
  327.     if( s = FindToolType( tt, "QUIET" ) )
  328.       result . noreq = (long *)s;
  329.  
  330.     ScanRDA();
  331. }
  332.  
  333. static
  334. void FreeInitProjectResult( void )
  335. {
  336.     /* NOP */
  337. }
  338.  
  339.  
  340. BOOL CreateBasicResources( void )
  341. {
  342.     return( OpenLibStuff() );
  343. }
  344.  
  345.  
  346. void DeleteBasicResources( void )
  347. {
  348.     CloseLibStuff();
  349. }
  350.  
  351.  
  352. BOOL OpenLibStuff( void )
  353. {
  354.     UtilityBase = IconBase = LocaleBase = NULL;
  355.  
  356.     if( IntuitionBase = OpenLibrary( "intuition.library", 37UL ) )
  357.     {
  358.       AttemptOpenLibrary( (&UtilityBase),  NAME, UTILITYNAME,      37UL );
  359.       AttemptOpenLibrary( (&IconBase),     NAME, ICONNAME,         37UL );
  360.       AttemptOpenLibrary( (&LocaleBase),   NAME, "locale.library", 38UL );
  361.  
  362.       if( LocaleBase )
  363.       {
  364.         ct = OpenCatalogA( NULL, NAME ".catalog", NULL );
  365.       }
  366.  
  367.       if( IconBase )
  368.       {
  369.         return( TRUE );
  370.       }
  371.     }
  372.  
  373.     CloseLibStuff();
  374.  
  375.     return( FALSE );
  376. }
  377.  
  378.  
  379. void CloseLibStuff( void )
  380. {
  381.     if( LocaleBase )
  382.     {
  383.       CloseCatalog( ct );
  384.  
  385.       CloseLibrary( LocaleBase );
  386.     }
  387.  
  388.     CloseLibrary( IconBase );
  389.     CloseLibrary( UtilityBase );
  390.     CloseLibrary( IntuitionBase );
  391. }
  392.  
  393.  
  394. void AttemptOpenLibrary( struct Library **library, STRPTR title, STRPTR libname, ULONG libversion )
  395. {
  396.     struct EasyStruct LibNotFoundES =
  397.     {
  398.       sizeof(struct EasyStruct),
  399.       0UL,
  400.       title,
  401.       SafeGetCatalogStr( ct, MSG_LIB_NOT_FOUND_REQTEXT, MSG_LIB_NOT_FOUND_REQTEXT_STR ),
  402.       SafeGetCatalogStr( ct, MSG_LIB_NOT_FOUND_REQGAD,  MSG_LIB_NOT_FOUND_REQGAD_STR )
  403.     };
  404.  
  405.     struct EasyStruct LibWrongVersionES =
  406.     {
  407.       sizeof(struct EasyStruct),
  408.       0UL,
  409.       title,
  410.       SafeGetCatalogStr( ct, MSG_LIB_WRONG_VERSION_REQTEXT, MSG_LIB_WRONG_VERSION_REQTEXT_STR ),
  411.       SafeGetCatalogStr( ct, MSG_LIB_WRONG_VERSION_REQGAD,  MSG_LIB_WRONG_VERSION_REQGAD_STR )
  412.     };
  413.  
  414.     if( (*library) == NULL )
  415.     {
  416.       for( ;; )
  417.       {
  418.         /* attemp to open shared library */
  419.         (*library) = OpenLibrary( libname, 0UL );
  420.  
  421.         if( *library )
  422.         {
  423.           if( ((*library) -> lib_Version) < libversion )
  424.           {
  425.             EasyRequest( NULL, (&LibWrongVersionES), NULL, libname, libversion );
  426.  
  427.             CloseLibrary( (*library) );
  428.             (*library) = NULL;
  429.           }
  430.  
  431.           break;
  432.         }
  433.  
  434.         /* prompt the user */
  435.         if( EasyRequest( NULL, (&LibNotFoundES), NULL, libname ) == 0L )
  436.         {
  437.           /* user canceled */
  438.           break;
  439.         }
  440.       }
  441.     }
  442. }
  443.  
  444.  
  445. void RunTool( void )
  446. {
  447.     struct EasyStruct RebootES =
  448.     {
  449.       sizeof(struct EasyStruct),
  450.       0UL,
  451.       NAME,
  452.       SafeGetCatalogStr( ct, MSG_REBOOT_REQTEXT, MSG_REBOOT_REQTEXT_STR ),
  453.       SafeGetCatalogStr( ct, MSG_REBOOT_REQGAD,  MSG_REBOOT_REQGAD_STR )
  454.     };
  455.  
  456.     if( project . noreq )
  457.     {
  458.       MyReboot();
  459.     }
  460.     else
  461.     {
  462.       if( EasyRequest( NULL, (&RebootES), NULL ) == 1L )
  463.       {
  464.         /* user wants to reboot */
  465.         MyReboot();
  466.       }
  467.       else
  468.       {
  469.         main_retval2 = ERROR_BREAK;
  470.         main_retval  = RETURN_WARN;
  471.       }
  472.     }
  473. }
  474.  
  475.  
  476. void MyReboot( void )
  477. {
  478.     struct Window *abortreqwindow;
  479.     ULONG          delay,
  480.                    x;
  481.     BOOL           abort;
  482.     ULONG          abortreqsignal,
  483.                    sigmask,
  484.                    signals;
  485.  
  486.     abort = FALSE;
  487.  
  488.     struct EasyStruct AbortRebootES =
  489.     {
  490.       sizeof(struct EasyStruct),
  491.       0UL,
  492.       NAME,
  493.       SafeGetCatalogStr( ct, MSG_ABORTREBOOT_REQTEXT, MSG_ABORTREBOOT_REQTEXT_STR )
  494.       SafeGetCatalogStr( ct, MSG_ABORTREBOOT_REQGAD,  MSG_ABORTREBOOT_REQGAD_STR )
  495.     };
  496.  
  497.     if( (project . rebootdelay) < 1UL )
  498.     {
  499.       project . rebootdelay = 1UL;
  500.     }
  501.  
  502.     if( project . noreq )
  503.     {
  504.       abortreqwindow = NULL;
  505.     }
  506.     else
  507.     {
  508.       /* Here we must check if we got the "low mem" return value (see BuildEasyRequestArgs autodoc)
  509.        * which may be 0 or 1
  510.        * For 0, I cannot determinate if this is a result from alert or a 'real' low mem failure
  511.        */
  512.       abortreqwindow = BuildEasyRequestArgs( NULL, (&AbortRebootES), 0UL, NULL );
  513.  
  514.       if( (abortreqwindow == ((struct Window *)1UL)) || (abortreqwindow == NULL) )
  515.       {
  516.         main_retval2 = ERROR_NO_FREE_STORE;
  517.         main_retval  = RETURN_FAIL;
  518.  
  519.         return;
  520.       }
  521.     }
  522.  
  523.     abortreqsignal = ((abortreqwindow)?(1UL << abortreqwindow -> UserPort -> mp_SigBit):(0UL));
  524.  
  525.     sigmask = SIGBREAKF_CTRL_C | abortreqsignal;
  526.  
  527.     delay = (project . rebootdelay) * 2UL;
  528.  
  529.     /* for each cycle we'll check if any abort signal arrived (CTRL_C or requester) */
  530.     for( x = 0UL ; x <= delay ; x++ )
  531.     {
  532.       /* check if user aborts countdown */
  533.       signals = CheckSignal( sigmask );
  534.  
  535.       if( signals & SIGBREAKF_CTRL_C )
  536.       {
  537.         abort = TRUE;
  538.       }
  539.  
  540.       /* no window, no signal, no event processing ... */
  541.       if( abortreqsignal )
  542.       {
  543.         if( signals & abortreqsignal )
  544.         {
  545.           if( SysReqHandler( abortreqwindow, NULL, FALSE ) == 0L )
  546.           {
  547.             abort = TRUE;
  548.           }
  549.         }
  550.       }
  551.  
  552.       if( abort )
  553.       {
  554.         break;
  555.       }
  556.  
  557.       /* wait a little bit */
  558.       Delay( (TICKS_PER_SECOND / 2UL) );
  559.     }
  560.  
  561.     /* Check if user has selected test mode using "Setenv Reboot OFF" */
  562.     if( abort == FALSE )
  563.     {
  564.       TEXT buff[ 256 ];
  565.  
  566.       /* check environment variable */
  567.       if( GetVar( NAME, buff, 255L, 0UL ) != (-1L) )
  568.       {
  569.         /* may be replaced by ReadArgs() */
  570.         if( !Stricmp( buff, "OFF" ) )
  571.         {
  572.           abort = TRUE;
  573.         }
  574.       }
  575.  
  576.       if( abortreqwindow )
  577.       {
  578.         /* if we got no abort msg, check the requester for any outstanding msg */
  579.         if( SysReqHandler( abortreqwindow, NULL, FALSE ) == 0L )
  580.         {
  581.           abort = TRUE;
  582.         }
  583.       }
  584.     }
  585.  
  586.     if( abort )
  587.     {
  588.       main_retval2 = ERROR_BREAK;
  589.       main_retval  = RETURN_WARN;
  590.     }
  591.     else
  592.     {
  593.       /* bite in the dust */
  594.       ColdReboot();
  595.     }
  596.  
  597.     /* user has aborted the rebooting sequence */
  598.     FreeSysRequest( abortreqwindow );
  599. }
  600.  
  601.  
  602. STRPTR SafeGetCatalogStr( struct Catalog *ct, LONG id, STRPTR s )
  603. {
  604.     if( LocaleBase )
  605.     {
  606.       s = GetCatalogStr( ct, id, s );
  607.     }
  608.  
  609.     return( s );
  610. }
  611.  
  612.  
  613.  
  614.